<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="user-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>

    <script type="text/html" id="user-toolbar">
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>

    <script type="text/html" id="user-bar">
        <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    </script>

    <script>
        layui.use(['table', 'form', 'jquery', 'popup'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let popup = layui.popup;

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center'
                },
                {
                    title: '用户名',
                    field: 'username',
                    align: 'center'
                },
                {
                    title: '昵称',
                    field: 'nickname',
                    align: 'center'
                },
                {
                    title: '状态',
                    field: 'status',
                    align: 'center',
                    templet: function(d) {
                        if (d.status == 1) {
                            return '<span class="layui-badge layui-bg-green">正常</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-gray">禁用</span>';
                        }
                    }
                },
                {
                    title: '创建时间',
                    field: 'create_time',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
                ]
            ];

            table.render({
                elem: '#user-table',
                url: '/api/user/list',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            // 定义局部函数
            const userAdd = function() {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    area: ['80%', '90%'],
                    maxmin: true,
                    content: '/user/add'
                });
            }

            const userEdit = function(obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    area: ['80%', '90%'],
                    maxmin: true,
                    content: '/user/edit?id=' + obj.data['id']
                });
            }

            const remove = function(obj) {
                layer.confirm('确定要删除该用户？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/api/user/delete",
                        data: {id: obj.data['id']},
                        dataType: 'json',
                        type: 'delete',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                popup.success(result.msg, function() {
                                    obj.del();
                                });
                            } else {
                                popup.failure(result.msg);
                            }
                        }
                    })
                });
            }

            const refresh = function() {
                table.reload('user-table');
            }

            const batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = data.map(item => item.id).join(',');
                layer.confirm('确定要删除这些用户？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/api/user/batchDelete/" + ids,
                        dataType: 'json',
                        type: 'delete',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                popup.success(result.msg, function() {
                                    table.reload('user-table');
                                });
                            } else {
                                popup.failure(result.msg);
                            }
                        }
                    })
                });
            }

            table.on('tool(user-table)', function(obj) {
                if (obj.event === 'remove') {
                    remove(obj);
                } else if (obj.event === 'edit') {
                    userEdit(obj);
                }
            });

            table.on('toolbar(user-table)', function(obj) {
                if (obj.event === 'add') {
                    userAdd();
                } else if (obj.event === 'refresh') {
                    refresh();
                } else if (obj.event === 'batchRemove') {
                    batchRemove(obj);
                }
            });

            form.on('submit(user-query)', function(data) {
                table.reload('user-table', {
                    where: data.field
                })
                return false;
            });
        });
    </script>
</body>
</html> 